package com.lh.pospi.pospi.login;

import android.app.Application;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;

/**
 * 登录逻辑
 */
public class LoginViewModel extends BaseViewModel {
    public ObservableField<String> userName = new ObservableField<>("");
    public ObservableField<String> pwd = new ObservableField<>("");
    public ObservableInt cleanNameStatus = new ObservableInt(View.VISIBLE);
    public ObservableInt cleanPwdStatus = new ObservableInt(View.INVISIBLE);
    public ObservableBoolean pwdShow = new ObservableBoolean(false);


    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    public BindingCommand showOrHidePwd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            pwdShow.set(!pwdShow.get());
        }
    });

    public BindingCommand cleanName = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });

    public BindingCommand cleanPwd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            pwd.set("");
        }
    });

    public BindingCommand loginBtn = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Toast.makeText(getApplication(), "点击了登陆按钮", Toast.LENGTH_SHORT).show();
        }
    });

    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> NameFocusChange = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                cleanNameStatus.set(View.VISIBLE);
            } else {
                cleanNameStatus.set(View.INVISIBLE);
            }
        }
    });
    //密码输入框焦点改变的回调事件
    public BindingCommand<Boolean> PwdFocusChange = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                cleanPwdStatus.set(View.VISIBLE);
            } else {
                cleanPwdStatus.set(View.INVISIBLE);
            }
        }
    });

}
